package cn.xinfei.xdecision.common.utils.util;

import org.springframework.util.StringUtils;

import java.util.regex.Pattern;

/**
 * 校验工具类
 *
 * @author 芋道源码
 */
public class ValidationUtils {

	private static final Pattern PATTERN_MOBILE = Pattern.compile("^(?:(?:\\+|00)86)?1(?:(?:3[\\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\\d])|(?:9[189]))\\d{8}$");

	private static final Pattern PATTERN_URL = Pattern.compile("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");

	private static final Pattern PATTERN_VALID_ENGINE_CODE = Pattern.compile("^(jcl_)[\\-_.0-9_a-zA-Z$]*");

	public static boolean isMobile(String mobile) {
		return StringUtils.hasText(mobile)
				&& PATTERN_MOBILE.matcher(mobile).matches();
	}

	public static boolean isURL(String url) {
		return StringUtils.hasText(url)
				&& PATTERN_URL.matcher(url).matches();
	}

	public static boolean isLegalEngineCode(String str) {
		return StringUtils.hasText(str)
				&& PATTERN_VALID_ENGINE_CODE.matcher(str).matches();
	}


//	public static void validate(Validator validator, Object object, Class<?>... groups) {
//		Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
//		if (CollUtil.isNotEmpty(constraintViolations)) {
//			throw new ConstraintViolationException(constraintViolations);
//		}
//	}

}
